//
//  NSArray+Filter.m
//  高仿暴走斗图
//
//  Created by key on 2017/3/8.
//  Copyright © 2017年 huangsongyao. All rights reserved.
//

#import "NSArray+Filter.h"

@implementation NSArray (Filter)

- (void)rac_filterUntilCompleted:(BOOL(^)(id predicate))completed toMap:(id(^)(id value))map subscribeNext:(void(^)(id x))next overFilter:(void(^)())over
{
    [[[[self.rac_sequence filter:^BOOL(id value) {
        if (completed) {
            return completed(value);
        }
        return YES;
    }] map:^id(id value) {
        if (map) {
            return map(value);
        }
        return value;
    }].signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        if (next) {
            next(x);
        }
    } completed:^{
        if (over) {
            over();
        }
    }];
}

- (void)rac_filterUntilCompleted:(BOOL(^)(id predicate))completed toMap:(id(^)(id value))map subscribeNext:(void(^)(id x))next
{
    [self rac_filterUntilCompleted:completed toMap:map subscribeNext:next overFilter:^{
        
    }];
}

- (RACSignal *)rac_filterUntilCompleted:(BOOL(^)(id predicate))completed toMap:(id(^)(id value))map
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [self rac_filterUntilCompleted:completed toMap:map subscribeNext:^(id x) {
            [subscriber sendNext:x];
        } overFilter:^{
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{
        }];
    }];
}

@end
